using System;
using System.Data;
using PCSComMaterials.Inventory.BO;
using PCSComProduction.WorkOrder.DS;
using PCSComUtils.Common;
using PCSComUtils.Common.BO;

using PCSComUtils.MasterSetup.DS;
using System.Transactions;
using PCSComUtils.DataContext;
using PCSComUtils.DataAccess;
using System.Collections.Generic;

namespace PCSComProduction.WorkOrder.BO
{
	public class Pro_IssueSubMaterialBO//:  IPro_IssueSubMaterialBO
	{
		public Pro_IssueSubMaterialBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}


		//[AutoComplete(true)]
		public void Add(object pObjectDetail)
		{
			throw new NotImplementedException();
		}

		
		//[AutoComplete(true)]
		public void Delete(object pObjectVO)
		{
			throw new NotImplementedException();
		}

		
		//[AutoComplete(true)]
		public object GetObjectVO(int pintID, string VOclass)
		{
			throw new NotImplementedException();
		}

		
		//[AutoComplete(true)]
		public void UpdateDataSet(DataSet dstData)
		{
			throw new NotImplementedException();
		}

		
		//[AutoComplete(true)]
		public void Update(object pObjectDetail)
		{
			throw new NotImplementedException();
		}

		//[AutoComplete(true)]
		public DataRow GetDefaultLocBin(int pintLocationID)
		{
			MST_BINDS dsLocation = new MST_BINDS();
			return dsLocation.GetDefaultLocBin(pintLocationID);
		}

		//[AutoComplete(true)]
		public string GetCodeFromID(int pintBinID)
		{
			MST_BINDS dsBin = new MST_BINDS();
			return dsBin.GetCodeFromID(pintBinID);
		}
        public bool InsertIssueSubMaster(PRO_IssueSubMaterialMaster objIssueSubMaster, List<PRO_IssueSubMaterialDetail> listDetail)
        {
            bool ret = true;
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {
                        db.PRO_IssueSubMaterialMasters.InsertOnSubmit(objIssueSubMaster);
                        db.SubmitChanges();
                        foreach (PRO_IssueSubMaterialDetail obj in listDetail)
                            obj.IssueSubMaterialMasterID = objIssueSubMaster.IssueSubMaterialMasterID;
                        db.PRO_IssueSubMaterialDetails.InsertAllOnSubmit(listDetail);
                        db.SubmitChanges();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                        ret = false;
                        Transaction.Current.Rollback(ex);
                    }
                    finally { trans.Complete(); }
                    return ret;
                }
            }
        }
		//[AutoComplete(true)]
		public int AddAndReturnID(PRO_IssueSubMaterialMasterVO voIssueSubMaterialMaster, DataSet dstData)
		{
			//add the master first
			Pro_IssueSubMaterialMasterDS objIssueSubMasterDS = new Pro_IssueSubMaterialMasterDS();
			int intIssueSubMaterialMasterID = objIssueSubMasterDS.AddAndReturnID(voIssueSubMaterialMaster);
				
			//update the id in the detail	
			DataSet dstempData = dstData.Clone();
			foreach (DataRow drow in dstData.Tables[0].Rows)
			{
				if (drow.RowState != DataRowState.Deleted)
				{
					DataRow newRow = dstempData.Tables[0].NewRow();
					newRow.ItemArray = drow.ItemArray;
					newRow[Pro_IssueSubMaterialDetailTable.ISSUESUBMATERIALMASTERID_FLD] = intIssueSubMaterialMasterID;
					dstempData.Tables[0].Rows.Add(newRow);
					drow[Pro_IssueSubMaterialDetailTable.ISSUESUBMATERIALMASTERID_FLD] = intIssueSubMaterialMasterID;
				}
			}
			
				
			//update the detail
			PRO_IssueSubMaterialDetailDS objIssueSubMaterialDetailDS = new PRO_IssueSubMaterialDetailDS();
			objIssueSubMaterialDetailDS.UpdateDataSet(dstempData);

			//Get detail data save RefDetailID 
			dstData = objIssueSubMaterialDetailDS.GetDetailData(intIssueSubMaterialMasterID);
			//End hack

			//Update the Inventory Database
			PRO_IssueSubMaterialMasterVO voIssueSubMaterial = voIssueSubMaterialMaster;
			MST_TransactionHistoryVO voMST_TransactionHistory = new MST_TransactionHistoryVO();
			MST_LocationVO voLocation = new MST_LocationVO();
			InventoryUtilsBO boInventoryUtils = new InventoryUtilsBO();

			//SaveTransactionHistory
			voMST_TransactionHistory.CCNID = voIssueSubMaterialMaster.CCNID;
			voMST_TransactionHistory.PostDate = voIssueSubMaterialMaster.PostDate;
			voLocation = (MST_LocationVO) new MST_LocationDS().GetObjectVO(voIssueSubMaterial.LocationId);
			voMST_TransactionHistory.MasterLocationID = voLocation.MasterLocationID;
				
			if (voMST_TransactionHistory.LocationID != 0)
				voMST_TransactionHistory.LocationID = voIssueSubMaterialMaster.LocationId;
			else
				voMST_TransactionHistory.LocationID =0;

			if (voIssueSubMaterialMaster.BinId != 0)
				voMST_TransactionHistory.BinID = voIssueSubMaterialMaster.BinId;
			else 
				voMST_TransactionHistory.BinID = 0;

			voMST_TransactionHistory.RefMasterID = intIssueSubMaterialMasterID;

			foreach (DataRow drowDaTa in dstData.Tables[0].Rows)
			{					
				UpdateInventory(drowDaTa, voIssueSubMaterialMaster);

				voMST_TransactionHistory.RefDetailID = int.Parse(drowDaTa[Pro_IssueSubMaterialDetailTable.ISSUESUBMATERIALDETAILID_FLD].ToString());
				voMST_TransactionHistory.ProductID = int.Parse(drowDaTa[Pro_IssueSubMaterialDetailTable.PRODUCTID_FLD].ToString());
				voMST_TransactionHistory.StockUMID = int.Parse(drowDaTa[Pro_IssueSubMaterialDetailTable.UMID_FLD].ToString());
				voMST_TransactionHistory.Quantity = -Decimal.Parse(drowDaTa[Pro_IssueSubMaterialDetailTable.QUANTITY_FLD].ToString());
				voMST_TransactionHistory.TransDate = new UtilsBO().GetDBDate();

				int iIssueSubMaterialTransID = (new UtilsBO()).GetTransTypeIDByCode(TransactionTypeEnum.PROIssueSubMaterial.ToString());
				//Save To Location TransHis
				boInventoryUtils.SaveTransactionHistory(TransactionType.PRO_ISSUE_SUB_MATERIAL, iIssueSubMaterialTransID,  voMST_TransactionHistory);				
			}

			//Get the data back in order to refresh the grid for later edit and delete	
			return intIssueSubMaterialMasterID;
		}

		//[AutoComplete(true)]
		private void UpdateInventory(DataRow pdrowData, PRO_IssueSubMaterialMasterVO voIssueSubMaterialMaster)
		{
			//define variable for the method of inventory
			decimal dcmCommitQty = 0;
			int pintMasterLocationID = 0;
			int intProductID = 0;

			#region GET Value for variables
			//get the Master location ID
			MST_LocationVO voLocation = new MST_LocationVO();
			voLocation = (MST_LocationVO) new MST_LocationDS().GetObjectVO(voIssueSubMaterialMaster.LocationId);
			pintMasterLocationID = voLocation.MasterLocationID;
				
			//get commit quantity
			if (pdrowData[Pro_IssueSubMaterialDetailTable.QUANTITY_FLD] != DBNull.Value
				&& pdrowData[Pro_IssueSubMaterialDetailTable.QUANTITY_FLD].ToString() != String.Empty)
			{
				dcmCommitQty = decimal.Parse(pdrowData[Pro_IssueSubMaterialDetailTable.QUANTITY_FLD].ToString());
			}

			//get Product ID
			if (pdrowData[Pro_IssueSubMaterialDetailTable.PRODUCTID_FLD] != DBNull.Value
				&& pdrowData[Pro_IssueSubMaterialDetailTable.PRODUCTID_FLD].ToString() != String.Empty)
			{
				intProductID = int.Parse(pdrowData[Pro_IssueSubMaterialDetailTable.PRODUCTID_FLD].ToString());
			}

			#endregion				

			new InventoryUtilsBO().UpdateSubtractOHQuantity(voIssueSubMaterialMaster.CCNID,
				pintMasterLocationID,
				voIssueSubMaterialMaster.LocationId,
				voIssueSubMaterialMaster.BinId,
				intProductID,
				dcmCommitQty,
				string.Empty,
				string.Empty);
			
		}

		//[AutoComplete(true)]
		public DataTable GetMasterIssue(int pintIssueMasterID)
		{
			return (new Pro_IssueSubMaterialMasterDS()).GetMasterIssue(pintIssueMasterID);
		}

		//[AutoComplete(true)]
		public DataSet GetDetailData(int pintMasterID)
		{
			PRO_IssueSubMaterialDetailDS dsIssueSubMaterialDetail = new PRO_IssueSubMaterialDetailDS();
			return dsIssueSubMaterialDetail.GetDetailData(pintMasterID);
		}

		//[AutoComplete(true)]
		public void DeleteIssueSubMaterial(int pintIssueSubMaterialMasterID)
		{
			int constTranTypeID = 27;
			int constinsStatus = 19;
			int constOldTranTypeID = (new MST_TranTypeDS()).GetTranTypeID(TransactionType.PRO_ISSUE_SUB_MATERIAL);
			//1. Get IssueSubMaterialMaster old Information 
			PRO_IssueSubMaterialMasterVO subMaterialMasterVO = new PRO_IssueSubMaterialMasterVO();
			subMaterialMasterVO = (PRO_IssueSubMaterialMasterVO) new Pro_IssueSubMaterialMasterDS().GetObjectVO(pintIssueSubMaterialMasterID);
			//2. add Quantity
            DataSet dsDetailIssueSubMaterial = new PRO_IssueSubMaterialDetailDS().GetDetailData(subMaterialMasterVO.IssueSubMaterialMasterId);
			foreach (DataRow drow in dsDetailIssueSubMaterial.Tables[0].Rows)
			{
				// Return Quantity
				Decimal decQuantity = Decimal.Parse(drow[Pro_IssueSubMaterialDetailTable.QUANTITY_FLD].ToString());
				//get the Master location ID
				MST_LocationVO voLocation = new MST_LocationVO();
				voLocation = (MST_LocationVO) new MST_LocationDS().GetObjectVO(subMaterialMasterVO.LocationId);
				int intMasterLocationID = voLocation.MasterLocationID;
				//ProductID
				int intProductID = int.Parse(drow[Pro_IssueSubMaterialDetailTable.PRODUCTID_FLD].ToString());

				new InventoryUtilsBO().UpdateAddOHQuantity(subMaterialMasterVO.CCNID,
					intMasterLocationID,
					subMaterialMasterVO.LocationId,
					subMaterialMasterVO.BinId,
					intProductID,
					decQuantity,
					string.Empty,
					string.Empty);				
			}
			// Delete row detail
			new PRO_IssueSubMaterialDetailDS().Delete(subMaterialMasterVO.IssueSubMaterialMasterId);
			// Delete row Master
			new Pro_IssueSubMaterialMasterDS().Delete(subMaterialMasterVO.IssueSubMaterialMasterId);

			//Update TransactionHistory
			new MST_TransactionHistoryDS().UpdateTranType(subMaterialMasterVO.IssueSubMaterialMasterId,constOldTranTypeID,constTranTypeID,constinsStatus);
		}

		//[AutoComplete(true)]
		public string GetUMCodeByID(int pintUMID)
		{
			MST_UnitOfMeasureVO voUM = new MST_UnitOfMeasureVO();
			voUM = (MST_UnitOfMeasureVO) new MST_UnitOfMeasureDS().GetObjectVO(pintUMID);
			if (voUM != null)
			{
				return voUM.Code;
			}
			return string.Empty;
		}
	}	
}
